<?php

namespace App\Http\Controllers\Api;

use App\Models\Filiale;
use App\Models\Company;
use Illuminate\Http\Request;
use App\Http\Requests\Api\FilialeRequest;
use App\Transformers\FilialeTransformer;

class FilialesController extends Controller
{
    public function search(Request $request, Filiale $filiale)
    {
        $per_page = $request->input('per_page', 20);
        $query = $filiale->query();

        $query->where('delflag', false);

        if ($name = $request->name) {
            $query=$query->where('name', 'like', "%{$request->name}%");
        }

        if ($phone = $request->phone) {
            $query=$query->where('phone', 'like', "%{$request->phone}%");
        }

        if ($address = $request->address) {
            $query=$query->where('address', 'like', "%{$request->address}%");
        }

        if ($company_id = $request->company_id) {
            $query=$query->where('company_id', $request->company_id);
        }

        if ($orderBy = $request->orderBy) {
            $query=$query->ordered();
        } else {
            $query=$query->recent();
        }

        $filiales = $query->paginate($per_page);

        return $this->response->paginator($filiales, new FilialeTransformer());
    }

    public function index(Request $request, Filiale $filiale)
    {
        $per_page = $request->input('per_page', 20);
        $query = $filiale->query();

        $query->where('delflag', false);

        if ($orderBy = $request->orderBy) {
            $query=$query->ordered();
        } else {
            $query=$query->recent();
        }

        $filiales = $query->paginate($per_page);

        return $this->response->paginator($filiales, new FilialeTransformer());
    }

    public function companyIndex(Request $request, Company $company)
    {
        $per_page = $request->input('per_page', 20);

        $filiales=null;

        if ($orderBy = $request->orderBy) {
            $filiales = $company->filiales()->where('delflag', false)->ordered()
                ->paginate($per_page);
        } else {
            $filiales = $company->filiales()->where('delflag', false)->recent()
                ->paginate($per_page);
        }


        return $this->response->paginator($filiales, new FilialeTransformer());
    }
    public function show(Company $company, Filiale $filiale)
    {
        return $this->response->item($filiale, new FilialeTransformer());
    }

	public function store(FilialeRequest $request, Company $company, Filiale $filiale)
	{
        $this->authorize('update', $filiale);
        $filiale->fill($request->all());

        $company->filiales()->save($filiale);

        return $this->response->item($filiale, new FilialeTransformer())
            ->setStatusCode(201);

	}


	public function update(FilialeRequest $request, Company $company, Filiale $filiale)
	{
        $this->authorize('update', $filiale);
		$filiale->update($request->all());

		return $this->response->item($filiale, new FilialeTransformer());
	}

	public function destroy(Company $company, Filiale $filiale)
	{
        $this->authorize('destroy', $filiale);
		$filiale->delflag=true;
        $filiale->save();
        return $this->response->array([
            'status'=>true,
            'data'=>[
                'successflag'=>true,
            ],
            'message'=>'删除成功',
        ])->setStatusCode(200);
	}

    public function attachCompany(Request $request, Filiale $filiale)
    {
        $this->authorize('update', $filiale);
        $filiale->company_id=$request->company_id;
        $filiale->save();
        return $this->response->array([
            'status'=>true,
            'data'=>[
                'successflag'=>true,
            ],
            'message'=>'商家分店关联成功',
        ])->setStatusCode(200);
    }
}